package com.ld.security.server.admin.modules.gl.vo;

import cn.hutool.core.io.IoUtil;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.server.admin.modules.gl.po.SpiceFilePo;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class SpiceFileVo {

    /**
     * 文件对象路径集合
     */
    private File[] files ;

    //文件编号  客户端提供
    private String fileId;

    //文件数量
    private int fileNumber;


    private int fileTotal;


    private String basePath;

    private File root;



    public SpiceFileVo(int fileNumber) {
        this.fileNumber = fileNumber;
        this.files = new File[fileNumber];
    }





    @SneakyThrows
    public synchronized boolean addFile(SpiceFilePo po){

        if(files == null){
            ApiException.throw400("上传超时,请重新上传文件");
        }
        File f =  new File(basePath+File.separator+po.getFileIndex());
        File p =f.getParentFile();
        if(!p.exists()){
            p.mkdirs();
        }
        po.getMultipartFile().transferTo(f);

        files[po.getFileIndex()]=f;
        fileTotal++;
        if(fileTotal == fileNumber){
            this.unionFile(root);
            return true;
        }

         //判断
         return  false;
    }


    /**
     * 加锁
     */
    public synchronized void deleteAll(){
        if(files == null){
            return;
        }
        for (File file : files) {
            file.delete();
        }
        files = null;

    }

    public File getRoot() {
        return root;
    }

    public void setRoot(File root) {
        this.root = root;
    }

    //文件合并

    @SneakyThrows
    private   File unionFile(File newFile){

        if(files == null){
            ApiException.throw400("上传超时,请重新上传文件");
        }


        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(newFile));

        byte[] bs = new byte[1024];

        for (File file : files) {


            BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));

            try{

                int readcount;
                while ((readcount = inputStream.read(bs)) > 0) {
                    outputStream.write(bs, 0, readcount);
                }




            }finally{
                inputStream.close();
            }

        }
        outputStream.flush();
        outputStream.close();
        this.deleteAll();

        return newFile;
    }


    public String getFileId() {
        return fileId;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public int getFileNumber() {
        return fileNumber;
    }

    public void setFileNumber(int fileNumber) {
        this.fileNumber = fileNumber;
    }




    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }


}
